package com.ysy.logistics.controller.supplier;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysy.common.myenum.StatusEnum;
import com.ysy.common.resp.ResultData;
import com.ysy.logistics.entities.supplier.Supplier;
import com.ysy.logistics.entities.supplier.dto.SupplierDto;
import com.ysy.logistics.entities.supplier.vo.SupplierQueryVO;
import com.ysy.logistics.mapper.supplier.SupplierMapper;
import com.ysy.logistics.myannotation.OperateLog;
import com.ysy.logistics.service.supplier.SupGoodsService;
import com.ysy.logistics.service.supplier.SupplierService;
import com.ysy.logistics.util.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @ClassDescription:
 * @Author:姚斯羽
 * @Create:2025/3/25 15:04
 **/
@Slf4j
@RestController
@RequestMapping(value = "/supplier",name = "供应商")
@Tag(name = "供应商管理",description = "supplier表的CRUD")
@CrossOrigin
public class SupplierController {

    @Resource
    private SupplierService supplierService;
    @Resource
    private SupGoodsService supGoodsService;

    @PostMapping( "/listByPage")
    @Operation(summary = "分页查询供应商",description = "分页查询供应商")
    @OperateLog("查询供应商")
    public ResultData listByPage(@RequestBody PageInfo<SupplierQueryVO> pageInfo) {
        Page<SupplierDto> page = supplierService.listByPage(pageInfo);
        return ResultData.success(page);
    }

    @PostMapping(value = "/save")
    @Operation(summary = "添加供应商",description = "添加供应商")
    @OperateLog("添加/修改 供应商")
    public ResultData add(@RequestBody Supplier supplier) {
       //参数校验
        if (StringUtils.isBlank(supplier.getId())){
            supplierService.save(supplier);
            return ResultData.success("新增供应商成功");
        }
        supplierService.updateById(supplier);
        return ResultData.success("修改供应商成功");
    }

    @GetMapping(value = "/changeStatus/{id}")
    @Operation(summary = "修改供应商状态",description = "修改供应商状态")
    @OperateLog("修改供应商状态")
    public ResultData changeStatus(@PathVariable("id") String id) {
        Supplier supplier = supplierService.getById(id);
        if (Objects.isNull(supplier)){
            return ResultData.fail("供应商不存在");
        }
        if (StringUtils.equals(supplier.getStatus(),StatusEnum.ENABLE.getCode())){
            supplier.setStatus(StatusEnum.DISABLE.getCode());
        }else {
            supplier.setStatus(StatusEnum.ENABLE.getCode());
        }
        Supplier update = new Supplier();
        update.setId(id);
        update.setStatus(supplier.getStatus());
        supplierService.updateById(update);
        return ResultData.success("修改供应商状态成功");
    }
    @GetMapping(value = "/delete/{id}")
    @Operation(summary = "删除供应商",description = "删除供应商")
    @OperateLog("删除供应商")
    public ResultData delete(@PathVariable("id") String id) {
        Supplier supplier = supplierService.getById(id);
        if (Objects.isNull(supplier)){
            return ResultData.fail("供应商不存在");
        }
        if (supplier.getStatus().equals(StatusEnum.ENABLE.getCode())){
            return ResultData.fail("供应商状态为启用，不能删除");
        }
        //删除供应商商品
        supGoodsService.removeBySupId(id);
        supplierService.removeById(id);
        return ResultData.success("删除供应商成功");
    }

    @GetMapping(value = "/getOnlySupNameList")
    @Operation(summary = "获取供应商名称列表",description = "获取供应商名称列表")
    public ResultData getOnlySupNameList() {
        LambdaQueryWrapper<Supplier> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Supplier::getName,Supplier::getId).eq(Supplier::getStatus,0);
        return ResultData.success(supplierService.list(queryWrapper));
    }
}
